package com.d16_Socket_Homework.homework3;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class ServerDemo3_TCP {
    public static void main(String[] args) throws IOException {
        //创建接收端对象，指定端口
        ServerSocket serverSocket = new ServerSocket(6666);
        //接收连接
        Socket socket = serverSocket.accept();
        //开流接收和写数据
        BufferedWriter bw = new BufferedWriter(
                new OutputStreamWriter(socket.getOutputStream())
        );
        BufferedReader br = new BufferedReader(
                new InputStreamReader(socket.getInputStream())
        );
        //接收数据并给予反馈
        String s;
        while ((s = br.readLine()) !=null) {
            System.out.println(s);
            //创建StringBuilder对象，完成字符串的反转
            StringBuilder str =  new StringBuilder();
            String string = str.append(s).reverse().toString();
            //写反馈
            bw.write(string);
            bw.newLine();
            bw.flush();
        }
    }
}
